from pydantic import BaseModel
from typing import Optional, List
from datetime import datetime


class NoteDocumentInfo(BaseModel):
    """笔记关联的文档信息"""
    id: str
    title: str
    file_name: str
    
    class Config:
        from_attributes = True


class NoteCreate(BaseModel):
    """创建笔记"""
    title: str
    content: str
    document_ids: List[str] = []
    knowledge_base_id: Optional[str] = None
    metadata: Optional[dict] = None


class NoteUpdate(BaseModel):
    """更新笔记"""
    title: Optional[str] = None
    content: Optional[str] = None
    document_ids: Optional[List[str]] = None
    knowledge_base_id: Optional[str] = None


class NoteResponse(BaseModel):
    """笔记响应"""
    id: str
    title: str
    content: str
    owner_id: str
    owner_name: Optional[str]
    knowledge_base_id: Optional[str]
    is_vectorized: bool
    is_public: bool
    word_count: int
    created_at: datetime
    updated_at: Optional[datetime]
    document_count: int = 0
    documents: List[NoteDocumentInfo] = []
    
    class Config:
        from_attributes = True


class NoteListItem(BaseModel):
    """笔记列表项（简化版，不包含完整内容）"""
    id: str
    title: str
    content_preview: str  # 内容预览（前200字）
    owner_id: str
    owner_name: Optional[str]
    knowledge_base_id: Optional[str]
    is_vectorized: bool
    is_public: bool
    word_count: int
    created_at: datetime
    updated_at: Optional[datetime]
    document_count: int = 0
    
    class Config:
        from_attributes = True


class NoteVersionResponse(BaseModel):
    """笔记版本响应"""
    id: str
    note_id: str
    title: str
    content: str
    version_number: int
    created_by: Optional[str]
    created_at: datetime
    
    class Config:
        from_attributes = True


class NoteVectorizeRequest(BaseModel):
    """笔记向量化请求"""
    knowledge_base_id: str


class NoteSaveToRequest(BaseModel):
    """保存AI回答到笔记的请求"""
    note_id: Optional[str] = None  # 如果为None则创建新笔记
    title: str
    content: str
    document_ids: List[str] = []
    metadata: Optional[dict] = None

